package com.example.mainstream.ui.user;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CompoundButton;

import com.example.shopapp.App;
import com.example.shopapp.R;
import com.example.shopapp.bean.ShopAddress;
import com.example.shopapp.database.ShopAddressDBUtils;
import com.example.shopapp.databinding.ActivityAddAddressBinding;
import com.example.shopapp.utils.Utils;
import com.lljjcoder.citypickerview.widget.CityPicker;

import org.greenrobot.eventbus.EventBus;

import cn.project.base.baseui.BaseNormalActivity;


/**
 * 文件名：AddAddressActivity
 * 描述：添加收获地址
 */
public class AddAddressActivity extends BaseNormalActivity<ActivityAddAddressBinding> {


    boolean isDefaulteCity = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initView() {
        init();
    }

    @Override
    protected void initData() {

    }

    protected void init() {
        mBinding.toolbar.tvTitle.setText("添加收货地址");
        mBinding.toolbar.rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBinding.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    isDefaulteCity = true;
                }else {
                    isDefaulteCity = false;
                }
            }
        });
        mBinding.tvAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseArea();
            }
        });
        mBinding.tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCity();
            }
        });

    }


    /**
     * 添加收货地址
     */
    private void addCity() {
        String name = mBinding.inputName.getText().toString();
        String address = mBinding.inputAddress.getText().toString();
        String mobile = mBinding.inputMobile.getText().toString();
        String chooseCity = mBinding.tvAddress.getText().toString();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(address) || TextUtils.isEmpty(mobile) || TextUtils.isEmpty(chooseCity)) {
            showToast("请完善收获地址信息");
        } else {
            ShopAddress shopAddress = new ShopAddress();
            String address_id = System.currentTimeMillis() + "";
            shopAddress.setAddress_id(address_id);
            if (isDefaulteCity) {
                Utils.address_id = "0";
                shopAddress.setDefaultcity("0");
            }else {
                Utils.address_id = "1";
                shopAddress.setDefaultcity("1");
            }
            shopAddress.setUser(name);
            shopAddress.setAddress(address);
            shopAddress.setCity(chooseCity);
            shopAddress.setUser_id(App.getInstance().user.getUser_id());
            shopAddress.setMobile(mobile);
            int i =   ShopAddressDBUtils.getInstance(getApplicationContext()).insert(shopAddress);
            if (i == 1){
                EventBus.getDefault().postSticky("添加地址");
                showToast("添加地址成功");
                finish();
            }
        }
    }

    public void chooseArea() {
        hideShowKeyboard();
        selectAddress();
    }

    /**
     * 软键盘显示/隐藏
     */
    public void hideShowKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); //得到InputMethodManager的实例
        if (imm.isActive()) {//如果开启
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);//关闭软键盘，开启方法相同，这个方法是切换开启与关闭状态的
        }
    }

    /**
     * 选择城市
     */
    private void selectAddress() {
        CityPicker cityPicker = new CityPicker.Builder(AddAddressActivity.this)
                .textSize(14)
                .title("地址选择")
                .titleBackgroundColor("#FFFFFF")
                .confirTextColor("#696969")
                .cancelTextColor("#696969")
                .province("北京市")
                .city("北京市")
                .district("东城区")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.show();
        //监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                //省份
                String province = citySelected[0];
                //城市
                String city = citySelected[1];
                //区县（如果设定了两级联动，那么该项返回空）
                String district = citySelected[2];
                //邮编
                String code = citySelected[3];
                //为TextView赋值
               mBinding.tvAddress.setText(province.trim() + "-" + city.trim() + "-" + district.trim());
            }
        });
    }


}

